﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using log4net;
using log4net.Repository.Hierarchy;
using Topshelf;

namespace Quartz.net
{
    //class Program
    //{
    //    //  public static DependencyResolverConfiguration DiResolver = new DependencyResolverConfiguration();
    //    private static readonly ILog Logger = log4net.LogManager.GetLogger("SampleLog");
    //    private static void Main(string[] args)
    //    {
    //        if (Environment.UserInteractive && Debugger.IsAttached)
    //        {
    //            RunAsConsole(args);
    //        }
    //        else
    //        {
    //            RunAsService();
    //        }
    //    }

    //    private static void RunAsService()
    //    {

    //        // Initialize Topshelf
    //        HostFactory.Run(hostConfigurator =>
    //        {
    //          ////  hostConfigurator.UseUnityContainer(DiResolver.Container);
    //          //  hostConfigurator.Service<WindowsService>(serviceConfigurator =>
    //          //  {
    //          //      // Let Topshelf use it
    //          //      serviceConfigurator.ConstructUsingUnityContainer();
    //          //      serviceConfigurator.WhenStarted(service => service.Start());
    //          //      serviceConfigurator.WhenStopped(service => { service.Stop(); });
    //          //      serviceConfigurator.WhenPaused(service => service.Pause());
    //          //      serviceConfigurator.WhenContinued(service => service.Continue());
    //          //  });

    //            hostConfigurator.RunAsLocalSystem();
    //            hostConfigurator.EnablePauseAndContinue();
    //            hostConfigurator.StartManually();
    //        });
    //    }
    //    private static void RunAsConsole(string[] args)
    //    {
    //        try
    //        {
    //            Logger.Info("Configure...");
    //            //  var service = DiResolver.Container.Resolve<WindowsService>();
    //            //service.Start();
    //            QuartzBase.CodeMethod().Wait();
    //            Logger.Info("Press any key to exit...");
    //            Console.ReadKey();
    //            Logger.Info("Stopping...");

    //            //service.Stop();
    //            Logger.Info("Successfully stopped!");
    //        }
    //        catch (Exception ex)
    //        {
    //            Logger.Error("Error occured");
    //          //  ExceptionCommonLogger.LogException(ex);
    //            Logger.Info("Press any key to exit...");
    //            Console.ReadKey();
    //        }
    //    }



    //}
    class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main(string[] args)
        {
            //如果传递了参数 s 就启动服务
            if (args.Length > 0 && args[0] == "s")
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] { new QuartzBase() };
                ServiceBase.Run(ServicesToRun);
            }
            else
            {
                Console.WriteLine("这是Windows应用程序");
                Console.WriteLine("请选择，[1]安装服务 [2]卸载服务 [3]退出");
                var rs = int.Parse(Console.ReadLine());
                string strServiceName = "syncService";
                switch (rs)
                {
                    case 1:
                        //取当前可执行文件路径，加上"s"参数，证明是从windows服务启动该程序
                        var path = Process.GetCurrentProcess().MainModule.FileName + " s";
                        Process.Start("sc", "create " + strServiceName + " binpath= \"" + path + "\" displayName= " + strServiceName + " start= auto");
                        Console.WriteLine("安装成功");
                        Console.Read();
                        break;
                    case 2:
                        Process.Start("sc", "delete " + strServiceName + "");
                        Console.WriteLine("卸载成功");
                        Console.Read();
                        break;
                    case 3: break;
                }

            }


        }
    }

}

